<template>
    <el-dialog
            :title="!dataForm.dataId ? '新增' : '修改'"
            :close-on-click-modal="false"
            :visible.sync="visible">
        <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="120px">
                <el-form-item label="车牌号" prop="licensePlate">
                        <el-input v-model="dataForm.licensePlate" placeholder="车牌号"></el-input>
                </el-form-item>
                <el-form-item label="定位时间" prop="locationTime">
                        <el-date-picker
                                v-model="dataForm.locationTime"
                                type="datetime"
                                placeholder="定位时间">
                        </el-date-picker>
                </el-form-item>
                <el-form-item label="经度" prop="longitude">
                        <el-input v-model="dataForm.longitude" placeholder="经度"></el-input>
                </el-form-item>
                <el-form-item label="纬度" prop="latitude">
                        <el-input v-model="dataForm.latitude" placeholder="纬度"></el-input>
                </el-form-item>
                <el-form-item label="速度(公里/小时)" prop="speed">
                        <el-input v-model="dataForm.speed" placeholder="速度(公里/小时)"></el-input>
                </el-form-item>
                <el-form-item label="车辆行驶方向" prop="direction">
                        <el-input v-model="dataForm.direction" placeholder="车辆行驶方向"></el-input>
                </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
    </el-dialog>
</template>

<script>
    export default {
      data () {
        return {
          visible: false,
          dataForm: {
            dataId: null,
            licensePlate: null,
            locationTime: null,
            longitude: null,
            latitude: null,
            speed: null,
            direction: null
          },
          dataRule: {
            dataId: [
                        { required: true, message: '主键ID，自动生成不能为空', trigger: 'blur' }
            ],
            licensePlate: [
                        { required: true, message: '车牌号，唯一标识车辆不能为空', trigger: 'blur' }
            ],
            locationTime: [
                        { required: true, message: '定位时间，记录车辆定位的具体时间不能为空', trigger: 'blur' }
            ],
            longitude: [
                        { required: true, message: '经度，定位的经度坐标不能为空', trigger: 'blur' }
            ],
            latitude: [
                        { required: true, message: '纬度，定位的纬度坐标不能为空', trigger: 'blur' }
            ],
            speed: [
                        { required: true, message: '速度，单位为公里/小时不能为空', trigger: 'blur' }
            ],
            direction: [
                        { required: true, message: '方向，车辆行驶的方向不能为空', trigger: 'blur' }
            ]
          }
        }
      },
      methods: {
        init (id) {
          this.dataForm.dataId = id || 0
          this.visible = true
          this.$nextTick(() => {
            this.$refs['dataForm'].resetFields()
            this.dataForm.dataId = id || 0
            if (this.dataForm.dataId) {
              this.$http({
                url: this.$http.adornUrl(`/app/vehicle_location_data/info/${this.dataForm.dataId}`),
                method: 'get',
                params: this.$http.adornParams()
              }).then(({data}) => {
                if (data && data.code === 0) {
                  this.dataForm = data.appvehiclelocationdata
                }
              })
            }
          })
        },
            // 表单提交
        dataFormSubmit () {
          this.$refs['dataForm'].validate((valid) => {
            if (valid) {
              this.$http({
                url: this.$http.adornUrl(`/app/vehicle_location_data/${!this.dataForm.dataId ? 'save' : 'update'}`),
                method: 'post',
                data: this.$http.adornData(this.dataForm)
              }).then(({data}) => {
                if (data && data.code === 0) {
                  this.$message({
                    message: '操作成功',
                    type: 'success',
                    duration: 1500,
                    onClose: () => {
                      this.visible = false
                      this.$emit('refreshDataList')
                    }
                  })
                } else {
                  this.$message.error(data.msg)
                }
              })
            }
          })
        }
      }
    }
</script>